<template>
  <div id="trans">
    <div class="top">
      <el-button size="mini" type="primary" plain @click="Button_on_add()">新建调拨单</el-button>
      <el-button size="mini" type="primary" plain @click="Button_on_edit()">编辑调拨单</el-button>
      <el-button
        size="mini"
        type="primary"
        plain
        :disabled="dis_delete"
        @click="Button_on_delete()"
      >删除调拨单</el-button>
      <el-button
        size="mini"
        type="primary"
        plain
        :disabled="dis_trans"
        @click="Button_on_trans()"
      >提交调拨单</el-button>

      <div class="top-check">
        <el-form :inline="true" :model="checkForm" size="mini">
          <el-form-item label="调拨单编码">
            <el-input v-model="checkForm.transRef"></el-input>
          </el-form-item>

          <el-form-item label="状态">
            <el-select v-model="checkForm.statu" placeholder="请选择状态">
              <el-option label="待调拨" value="待调拨"></el-option>
              <el-option label="已调拨" value="已调拨"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务日期">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="checkForm.date"
              value-format="yyyy-MM-dd"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="CheckSubmit">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="CheckClear">清空查询条件</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="body">
      <el-table
        border
        :data="trans_list"
        :stripe="true"
        @row-click="selectRow"
        :header-cell-style="{background:'#e3e5e9',color:'#606266'}"
        :row-style="{height:'5px'}"
        :cell-style="{padding:'5px 0'}"
        tooltip-effect="light"
        highlight-current-row
        height="400"
      >
        <el-table-column label="序号" width="50">
          <template slot-scope="scope">
            <span>{{scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="statu" label="状态" width="95"></el-table-column>
        <el-table-column prop="transRef" label="编号" width="200"></el-table-column>
        <el-table-column prop="date" label="业务日期" width="205"></el-table-column>
        <el-table-column prop="storeOName" label="调出仓库" width="205"></el-table-column>
        <el-table-column prop="storeIName" label="调入仓库" width="205"></el-table-column>
        <el-table-column prop="bizname" label="业务员" width="150"></el-table-column>
      </el-table>
    </div>
    <div class="detail">
      <el-tabs type="border-card">
        <el-tab-pane>
          <span slot="label">
            <i class="el-icon-s-order"></i>
            单号:{{ref}} 调出仓库:{{out_storename}} 调入仓库:{{inter_storename}}
          </span>
          <div>
            <el-table
              border
              :data="detaillist"
              size="mini"
              :row-style="{height:'5px'}"
              :cell-style="{padding:'5px 0'}"
            >
              <el-table-column label="序号" width="50">
                <template slot-scope="scope">
                  <span>{{scope.$index + 1}}</span>
                </template>
              </el-table-column>
              <el-table-column label="商品编码" width="250" prop="code"></el-table-column>
              <el-table-column label="商品名称" prop="name" width="200"></el-table-column>
              <el-table-column show-overflow-tooltip prop="spec" label="规格型号" width="250"></el-table-column>
              <el-table-column show-overflow-tooltip prop="count" label="调拨数量" width="95"></el-table-column>
              <el-table-column show-overflow-tooltip label="单位" prop="unitname" width="125"></el-table-column>
              <el-table-column prop="memo" show-overflow-tooltip label="备注" width="410"></el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <Add
      v-if="visible_add"
      @dialog-close="visible_add = false"
      @refresh="refreshTransList"
      :form="selectedRow"
    ></Add>
    <Edit
      v-if="visible_edit"
      @dialog-close="visible_edit = false"
      @refresh="refreshTransList"
      :form="selectedRow"
    ></Edit>
    <div class="delete">
      <el-dialog
        title="确定删除"
        :visible.sync="visible_delete"
        :modal-append-to-body="false"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
      >
        <div class="notify">
          <p>
            <strong>注意：</strong>
            <br />确定归还后，不能删除该盘点单
          </p>
        </div>
        <p>
          你确定要删除
          <strong>{{selectedRow.transRef}}</strong>
          吗？
        </p>

        <div slot="footer" class="dialog-footer">
          <el-button @click="visible_delete = false">取 消</el-button>
          <el-button type="primary" @click="Submit_Delete()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="submit">
      <el-dialog
        title="确定调拨"
        :visible.sync="visible_trans"
        :modal-append-to-body="false"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
      >
        <p>
          你确定要提交调拨单
          <strong>{{selectedRow.transRef}}</strong>
          吗？
        </p>

        <div slot="footer" class="dialog-footer">
          <el-button @click="visible_trans = false">取 消</el-button>
          <el-button type="primary" @click="Submit_trans()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Add from "./trans_dialog/add";
import Edit from "./trans_dialog/edit";
export default {
  components: {
    Add,
    Edit
  },
  data() {
    return {
      ref: "IT151516",
      out_storename: "IT151516",
      inter_storename: "IT151516",
      visible_add: false,
      visible_delete: false,
      visible_edit: false,
      visible_trans: false,
      checkForm: {},
      selectedRow: {},
      trans_list: [],
      detaillist: [],
      dis_delete: false,
      dis_trans: false
    };
  },
  created() {
    this.refreshTransList();
  },
  methods: {
    CheckClear() {
      this.checkForm = {};
    },
    CheckSubmit() {
      this.$http.post("assetTrans/search", this.checkForm).then(response => {
        this.trans_list  = response.data;
      });
    },
    selectRow(Row) {
      window.console.log(Row);
      this.selectedRow = JSON.parse(JSON.stringify(Row));
      this.ref = Row.transRef;
      this.out_storename = Row.storeOName;
      this.inter_storename = Row.storeIName;
      this.$http
        .post("assetTransDetail/list", { transId: Row.id })
        .then(response => {
          this.detaillist = response.data;
        });

      if (Row.statu == "待调拨") {
        this.dis_delete = false;
        this.dis_trans = false;
      } else if (Row.statu == "已调拨") {
        this.dis_delete = true;
        this.dis_trans = true;
      }
    },
    Button_on_add() {
      this.visible_add = true;
    },
    Button_on_edit() {
      this.visible_edit = true;
    },
    Button_on_delete() {
      this.visible_delete = true;
    },
    Button_on_trans() {
      this.visible_trans = true;
    },
    refreshTransList() {
      this.$http.get("assetTrans/list").then(response => {
        this.trans_list = response.data;
      });
    },
    Submit_Delete() {
      this.$http
        .post("assetTrans/delete", { id: this.selectedRow.id })
        .then(response => {
          if (response.data == "success") {
            this.$message({
              type: "success",
              message: "删除完成"
            });
            this.visible_delete = false;
            this.refreshTransList();
          }
        });
    },
    Submit_trans() {
      let cstoreid = this.selectedRow.storeO;
      let count = 0;
      let vm = this;
      this.detaillist.forEach(Row => {
        Row.storeid = cstoreid;
        Row.goodcode = Row.code;
        Row.goodname = Row.name;
        Row.statu = "在库";
        this.$http
          .post("good/someGoodList", { code: Row.code })
          .then(response => {
            Row.zijin = response.data[0].saleprice * Row.count;
            count--;
            if (count == vm.detaillist.length) {
              this.$http
                .post("assetDetail/check", vm.detaillist)
                .then(response => {
                  if (response.data == false) {
                    this.$message.error("仓库库存量不够用!");
                    return;
                  } else if (response.data == true) {
                    this.detaillist.forEach(row => {
                      row.storeid = this.selectedRow.storeO;
                      row.statu = "在库";
                      this.$http
                        .post("assetDetail/insert", row)
                        .then(response => {
                          if (response.data == "success")
                            this.detaillist.forEach(row => {
                              row.storeid = this.selectedRow.storeI;
                              row.statu = "调出";
                              this.$http.post("assetDetail/insert", row);
                            });
                        });
                    });
                    this.selectedRow.statu = "已调拨";
                    this.$http
                      .post("assetTrans/update", this.selectedRow)
                      .then(response => {
                        if (response.data == this.selectedRow.id) {
                          
                          this.$message({
                            type: "success",
                            message: "提交完成"
                          });
                          this.visible_trans = false;
                          this.refreshTransList();
                        }
                      });
                  }
                });
            }
          });
      });

      window.console.log("sublist", this.detaillist);
    }
  }
};
</script>

<style>
</style>